package com.youkeda.excise;

import java.util.LinkedList;

/**
 * @author CoderXshuai
 * @date 2020/7/13/0013 23:08
 */

public class MyLinkedList {
    private ListNode head = new ListNode(0);
    private ListNode rear = head;
    private int size = 0;

    public MyLinkedList() {
        LinkedList<String> linkedList = new LinkedList<>();
        //迭代器
        for (String s : linkedList) {
        }
        //下标递增
        for (int i = 0; i < linkedList.size(); i++) {
        }
    }

    public int size() {
        return this.size;
    }

    //默认增加到最后
    public void add(int value) {
        //新建一个节点
        ListNode node = new ListNode(value);
        //如果插入的是当前链表的第一个节点
        if (head == rear) {
            head.next = node;
            rear = node;
            size++;
            return;
        }
        //不是第一个节点
        rear.next = node;
        rear = node;
        this.size++;
    }

    public int get(int index) {
        ListNode result = head;
        //需要遍历index+1次,因此范围为[0,index]
        for (int i = 0; i <= index; i++) {
            result = result.next;
        }
        return result.value;
    }

    public void remove(int index) {
        //下标判断
        if (index < 0 || index >= size) {
            return;
        }
        ListNode temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        temp.next = temp.next.next;
        this.size--;
    }

}

class ListNode {
    //数值
    int value;
    //下一个节点的指针
    ListNode next;

    ListNode(int x) {
        value = x;
    }
}
